package com.yunke.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * 云科定时任务配置类
 * 
 * @author yunke
 * @since 2024-01-01
 */
@Configuration
@EnableScheduling
public class YunkeScheduleConfig {

    /**
     * 定时任务线程池配置
     */
    @Bean("taskScheduler")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        
        // 核心线程数
        scheduler.setPoolSize(5);
        
        // 线程名前缀
        scheduler.setThreadNamePrefix("yunke-schedule-");
        
        // 拒绝策略：调用者运行
        scheduler.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        
        // 等待任务完成后再关闭线程池
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        
        // 等待时间
        scheduler.setAwaitTerminationSeconds(60);
        
        // 初始化
        scheduler.initialize();
        
        return scheduler;
    }
} 